package com.hudongwx.food.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.hudongwx.food.R;
import com.hudongwx.food.activity.TiaoZhuanZhuanTiActivity;

import org.json.JSONArray;
import org.json.JSONException;
import org.xutils.x;

/**
 * Author:降初
 * DateTime:16-12-14 上午10:41
 */
public class MyListViewAdapter2 extends BaseAdapter{
    Context context;
    JSONArray listview2;
    public MyListViewAdapter2(JSONArray listview2,Context context) {
        this.listview2 = listview2;
        this.context = context;
    }



    @Override
    public int getCount() {
        return listview2.length();
    }

    @Override
    public Object getItem(int position) {
        return getItem(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        MyViewHolder2 myViewHolder2;
        if (null==convertView){
            myViewHolder2 = new MyViewHolder2();
            convertView = LayoutInflater.from(context).inflate(R.layout.home_topics_1,null);
            myViewHolder2.iv = (ImageView) convertView.findViewById(R.id.home_topics);
            myViewHolder2.tv = (TextView) convertView.findViewById(R.id.home_topics_tv);
            convertView.setTag(myViewHolder2);
        }else {
            myViewHolder2= (MyViewHolder2) convertView.getTag();
        }
        try {
            x.image().bind(myViewHolder2.iv,listview2.getJSONObject(position).getString("CoverUrl"));
            myViewHolder2.tv.setText(listview2.getJSONObject(position).getString("Title"));
            myViewHolder2.iv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context, TiaoZhuanZhuanTiActivity.class);
                    int i = 0;
                    try {
                        i = listview2.getJSONObject(position).getInt("Id");
                        intent.putExtra("type",i);
                        context.startActivity(intent);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            });
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return convertView;
    }

    class MyViewHolder2{
        ImageView iv;
        TextView tv;
    }
}
